package com.task.core.service.impl;

import java.util.List;
import javax.annotation.Resource;

import com.task.common.redis.RedisKeyConstants;
import com.task.core.vo.resp.CountryDetailRespVo;
import com.task.core.vo.resp.DatasourceDetailRespVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.task.common.annotation.DataScope;
import com.task.common.core.redis.RedisCache;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.exception.ServiceException;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.StringUtils;
import com.task.common.utils.uuid.UUID;
import com.task.common.domain.info.CountryInfo;
import com.task.core.dto.request.CountryRequestDTO;
import com.task.core.mapper.CountryMapper;
import com.task.core.service.CountryService;

/**
 * @author 迪迦.
 * @date 2024/8/14 15:09
 */
@Service("CountryService")
public class CountryServiceImpl implements CountryService {

  @Resource
  private CountryMapper countryMapper;

  @Resource
  private RedisCache redisCache;

  @Override
  public void addCountry(CountryRequestDTO request) {
    if (StringUtils.isEmpty(request.getCountryName())) {
      throw new ServiceException("国家名称不能为空");
    }
    //国家是通用的,添加时需要判断是否存在
    if (!ObjectUtils.isEmpty(getCountryByName(request.getCountryName()))) {
      throw new ServiceException("国家已存在");
    }
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    countryMapper.addCountry(request);
    addCountryCache(request);
  }

  private void addCountryCache(CountryRequestDTO request) {
    CountryInfo countryInfo = new CountryInfo();
    countryInfo.setCode(request.getCode());
    countryInfo.setCountryName(countryInfo.getCountryName());
    countryInfo.setDescription(request.getDescription());
    redisCache.setCacheObject(RedisKeyConstants.COUNTRY_INFO.formatKey(request.getCode()), countryInfo);
  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<CountryInfo> getCountryByPage(CountryRequestDTO request) {
    return countryMapper.getCountryByPage(request);
  }

  @Override
  public CountryInfo getCountryByName(String countryName) {
    return countryMapper.getCountry(countryName, null);
  }

  @Override
  public CountryInfo getCountryByCode(String countryCode) {
    return countryMapper.getCountry(null, countryCode);
  }

  @Override
  public void deleteCountry(String countryCode, Long operationBy) {
    Integer delete = countryMapper.deleteCountry(countryCode, operationBy);
    if(delete == 0) {
      throw new ServiceException("code不存在");
    }
    redisCache.deleteObject(RedisKeyConstants.COUNTRY_INFO.formatKey(countryCode));
  }

  @Override
  public void modifyCountry(CountryRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    Integer integer = countryMapper.modifyCountry(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    addCountryCache(request);
  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getCountryForOption(CountryRequestDTO request) {
    return countryMapper.getCountryForOption(request);
  }

  @Override
  @DataScope(deptAlias = "dept",userAlias = "su")
  public CountryDetailRespVo getCountryDetail(String countryCode) {
    CountryRequestDTO countryRequestDTO = new CountryRequestDTO();
    countryRequestDTO.setCode(countryCode);
    return countryMapper.getCountryDetail(countryRequestDTO);
  }
}
